package com.xh.audit.service.impl;


import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.xh.audit.entity.auditdocument.AuditDocuments;
import com.xh.audit.entity.project.ProjectArchive;
import com.xh.audit.entity.project.table.ProjectArchiveTableDef;
import com.xh.audit.mapper.AuditDocumentsMapper;
import com.xh.audit.service.AuditDocumentsService;
import com.xh.audit.service.ProjectArchiveService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 审计文书 服务实现类
 * </p>
 *
 * @author lhl
 * @since 2023-10-19
 */
@RequiredArgsConstructor
@Service
public class AuditDocumentsServiceImp extends ServiceImpl<AuditDocumentsMapper, AuditDocuments> implements AuditDocumentsService {

    private final ProjectArchiveService projectArchiveService;


    @Transactional
    @Override
    public void del(List<Integer> ids) {
        this.removeByIds(ids);
        //一条文书对应一条档案,档案大于文书;
        QueryWrapper queryWrapper = QueryWrapper.create().and(ProjectArchiveTableDef.PROJECT_ARCHIVE.AUDIT_DOCUMENT_ID.in(ids));
        projectArchiveService.remove(queryWrapper);

    }

    /**
     * 审计文书保存 要记录档案
     *
     * @param tosave
     * @return
     */
    @Transactional
    @Override
    public boolean saveAuditDocuments(AuditDocuments tosave) {
        boolean saveProjectArchiveFlag = false;
        if (null == tosave.getId()) {
            saveProjectArchiveFlag = true;
        }
        boolean saved = this.saveOrUpdate(tosave);

        if (saveProjectArchiveFlag) {
            ProjectArchive build = ProjectArchive.builder()
                    .auditDocumentFlag(1)
                    .projectId(tosave.getProjectId())
                    .auditDocumentId(tosave.getId())
                    .flowNode(tosave.getFlowNode())
                    .build();
            projectArchiveService.save(build);
        }

        return saved;
    }
}
